package com.sad.we11cis;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	EditText idText, passwordText;
	Button signUpBtn, loginBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		loginBtn = (Button)findViewById(R.id.btnLogin);
		signUpBtn = (Button)findViewById(R.id.btnSignUp);
		idText = (EditText)findViewById(R.id.userID);
		passwordText = (EditText)findViewById(R.id.userPassword);

		loginBtn.setOnClickListener(this);
		signUpBtn.setOnClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnLogin:
			LoginTask loginTask = new LoginTask();
			loginTask.execute();
			break;
		case R.id.btnSignUp:
			Intent goSignUpPage = new Intent(MainActivity.this, ReviewRegister.class);
			startActivity(goSignUpPage);
			break;
		}
	}

	private class LoginTask extends AsyncTask<Void, Void, Void>{

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				URL url = new URL("http://192.168.43.64:8080/Weening11evenServer/CommunicateServlet");
				URLConnection connection = url.openConnection();

				//send ID and password to server
				String idString = idText.getText().toString();
				String passwordString = passwordText.getText().toString();
				connection.setDoOutput(true);
				OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
				out.write("For Login/" + idString + "/" + passwordString);
				out.close();

				//receive result of login from server
				String userRank = "";
				BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				userRank = in.readLine();
				in.close();

				if(userRank.equals("admin")){
					
				}
				else if(userRank.equals("customer")){
					Intent customerPage = new Intent(MainActivity.this, CustomerPage.class);
					startActivity(customerPage);
				}
				else if(userRank.equals("marketer")){
					Intent marketerPage = new Intent(MainActivity.this, MarketerPage.class);
					marketerPage.putExtra("marketerID", idString);
					startActivity(marketerPage);
				}
				else{
					publishProgress();
				}

			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);

			Toast wrongAccessMessage = Toast.makeText(getApplicationContext(), "Invalid ID or Password", Toast.LENGTH_SHORT);
			wrongAccessMessage.show();
		}

	}

}
